package com.hiray.util

import android.app.ActivityManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.StrictMode
import android.widget.Toast
import androidx.core.content.FileProvider
import com.example.index.BuildConfig
import com.example.index.R
import es.dmoral.toasty.Toasty

import java.io.File


class ShareHelper {

    // 微信好友
    // package = com.tencent.mm,
    // activity = com.tencent.mm.ui.tools.ShareImgUI
    // 微信朋友圈
    // package = com.tencent.mm,
    // activity = com.tencent.mm.ui.tools.ShareToTimeLineUI
    //
    // package = com.tencent.mobileqq,
    // activity = com.tencent.mobileqq.activity.JumpActivity
    // package = com.tencent.mobileqq,
    // activity = com.tencent.mobileqq.activity.qfileJumpActivity
    // QQ空间
    // package = com.qzone, activity =
    // com.qzonex.module.operation.ui.QZonePublishMoodActivity
    // 人人
    // package = com.renren.mobile.android,
    // activity = com.renren.mobile.android.publisher.UploadPhotoEffect
    // 陌陌
    // package = com.immomo.momo, activity =
    // com.immomo.momo.android.activity.feed.SharePublishFeedActivity
    // 新浪微博
    // package = com.sina.weibo, activity = com.sina.weibo.EditActivity
    companion object ShareHelperSOUL {

        val PKG_TIM = "com.tencent.tim"

        val PKG_QQ = "com.tencent.mobileqq"

        val PKG_WECHAT = "com.tencent.mm"

        fun isInstalled(context: Context, packageName: String, activityName: String): Boolean {

            val packages = context.packageManager.getInstalledPackages(0)
            packages.forEach {
                if (it.packageName == packageName)
                    return true
            }
            return false
        }

        fun shareToWeChatFriend(context: Context, file: File) {
            share(false, context, file, "com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI")
        }

        fun shareToWeChatTimeLine(context: Context, file: File) {
            share(
                false,
                context,
                file,
                "com.tencent.mm",
                "com.tencent.mm.ui.tools.ShareToTimeLineUI"
            )
        }

        fun shareToQQFriend(context: Context, file: File) {
            if (isInstalled(
                    context,
                    PKG_QQ,
                    "com.tencent.mobileqq.activity.JumpActivity"
                )
            )
                share(
                    context,
                    file,
                    PKG_QQ,
                    "com.tencent.mobileqq.activity.JumpActivity"
                )
            else {//尝试tim分享
                if (isInstalled(
                        context,
                        "com.tencent.tim",
                        "com.tencent.mobileqq.activity.JumpActivity"
                    )
                )
                    share(
                        context,
                        file,
                        "com.tencent.tim",
                        "com.tencent.mobileqq.activity.JumpActivity"
                    )
            }
        }

        fun shareToTimOrQQ(context: Context, file: File, pkgName: String) {

            if (isInstalled(
                    context,
                    "com.tencent.tim",
                    "com.tencent.mobileqq.activity.JumpActivity"
                )
            ) {

                simpleShareFileAndMsg(context, "com.tencent.tim", pkgName, file)

            } else if (isInstalled(
                    context,
                    PKG_QQ,
                    "com.tencent.mobileqq.activity.JumpActivity"
                )
            ) {

                simpleShareFileAndMsg(context, PKG_QQ, pkgName, file)

            } else {//尝试tim分享
                Toasty.warning(context, R.string.tencent_app_not_exist, Toast.LENGTH_LONG).show()
            }
        }


        fun simpleShareFileAndMsg(
            context: Context,
            pkgName: String,
            msg: String,
            file: File
        ) {
            val uri =
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) FileProvider.getUriForFile(
                    context,
                    BuildConfig.APPLICATION_ID,
                    file
                ) else Uri.fromFile(file)

            val shareIntent = Intent()
            shareIntent.action = Intent.ACTION_SEND_MULTIPLE
//            shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, arrayListOf(uri))
//            shareIntent.putParcelableArrayListExtra(Intent.EXTRA_TEXT, arrayListOf(msg))
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
            shareIntent.putExtra(Intent.EXTRA_TEXT, msg)
            shareIntent.putExtra(Intent.EXTRA_TITLE, pkgName)
            shareIntent.setDataAndType(uri, "*/*")
            shareIntent.`package` = pkgName

            shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

            context.startActivity(shareIntent)
        }

        fun shareToQQZone(context: Context, file: File) {
            share(
                context,
                file,
                "com.qzone",
                "com.qzonex.module.operation.ui.QZonePublishMoodActivity"
            )
        }

        fun shareToWeibo(context: Context, file: File) {
            var packageName: String = "com.sina.weibo"
            if (!isInstalled(context, packageName, "")) {
                packageName = "com.weico.international"
                if (!isInstalled(context, packageName, "")) {
                    Toasty.info(context, "未安装该应用").show()

                    return
                }
            }
            val intent = Intent()

            val uri = Uri.fromFile(file)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//                if (false) {
//                    intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
//                    uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file)
//                } else {
                StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder().build())
//                }
            }

            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            intent.putExtra(Intent.EXTRA_STREAM, uri)
            intent.action = Intent.ACTION_SEND
            intent.type = "image/*"
//            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file))
//            intent.putExtra(Intent.EXTRA_TEXT, "分享下载吧")
            intent.`package` = packageName
            context.startActivity(intent)
        }


        fun share(
            b: Boolean,
            context: Context,
            file: File,
            packageName: String,
            componentName: String
        ) {
            if (!isInstalled(context, packageName, componentName)) {
                Toasty.info(context, "未安装该应用").show()
                return
            }
            val intent = Intent()

            var uri = Uri.fromFile(file)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file)
                if (b) {
                    intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
                } else {
                    StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder().build())
                }
            }

            if (isAlive(context, packageName).not())
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            intent.putExtra(Intent.EXTRA_STREAM, uri)
            intent.action = Intent.ACTION_SEND
            intent.setDataAndType(uri, "image/*")
//            intent.type = "image/*"
//            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file))
//            intent.putExtra(Intent.EXTRA_TEXT, "分享下载吧")
            intent.component = ComponentName(packageName, componentName)
            context.startActivity(intent)
        }

        private fun specialProcess(intent: Intent, packageName: String) {
            if (packageName == "com.sina.weibo")
                intent.`package` = packageName
        }


        fun share(context: Context, file: File, packageName: String, componentName: String) {
            share(false, context, file, packageName, componentName)
        }

        fun isAlive(context: Context, packageName: String): Boolean {
            val activityManager =
                context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            val runningAppProcesses = activityManager.runningAppProcesses
            runningAppProcesses.forEach {
                if (it.processName == packageName)
                    return true
            }
            return false
        }

    }
}